package com.wisdom.benefit.jufa.widget;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.util.TypedValue;

import androidx.annotation.ColorInt;
import androidx.appcompat.widget.AppCompatTextView;

import com.wisdom.benefit.jufa.R;

/**
 * 用来添加提示（点）
 * Created by zhoudawei on 2018/1/9.
 */

public class PointTextView extends AppCompatTextView {

    private int mPaddingHorizontal;
    private int mPaddingVertical;
    private int mPointDiameter;
    @ColorInt
    private int mPointColor;
    private Paint mPaint;

    public PointTextView(Context context) {
        this(context, null);
    }

    public PointTextView(Context context, AttributeSet attrs) {
        this(context, attrs, android.R.attr.textViewStyle);
    }

    public PointTextView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        DisplayMetrics metrics = context.getResources().getDisplayMetrics();
        TypedArray array = context.obtainStyledAttributes(attrs, R.styleable.PointTextView, defStyleAttr, 0);
        mPaddingHorizontal = array.getDimensionPixelSize(R.styleable.PointTextView_padding_horizontal,
                (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 8, metrics));
        mPaddingVertical = array.getDimensionPixelSize(R.styleable.PointTextView_padding_vertical,
                (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 8, metrics));
        mPointDiameter = array.getDimensionPixelSize(R.styleable.PointTextView_point_diameter,
                (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 8, metrics));
        mPointColor = array.getColor(R.styleable.PointTextView_point_color, Color.TRANSPARENT);
        array.recycle();
        mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        mPaint.setStyle(Paint.Style.FILL_AND_STROKE);
        mPaint.setStrokeWidth(mPointDiameter);
        mPaint.setStrokeCap(Paint.Cap.ROUND);
        mPaint.setColor(mPointColor);
    }

    public PointTextView setPaddingHorizontal(int paddingHorizontal) {
        mPaddingHorizontal = paddingHorizontal;
        return this;
    }

    public PointTextView setPaddingVertical(int paddingVertical) {
        mPaddingVertical = paddingVertical;
        return this;
    }

    public PointTextView setPointDiameter(int pointDiameter) {
        mPointDiameter = pointDiameter;
        mPaint.setStrokeWidth(mPointDiameter);
        return this;
    }

    public PointTextView setPointColor(@ColorInt int pointColor) {
        mPointColor = pointColor;
        mPaint.setColor(mPointColor);
        return this;
    }

    public void update() {
        postInvalidate();
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        int x = getWidth() - mPaddingHorizontal;
        int y = mPaddingVertical;
        canvas.drawPoint(x, y, mPaint);
    }
}
